package com.duyi.demo.data;

import com.duyi.demo.bean.DataBean;
import com.google.gson.Gson;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author ctx
 * @date 2020/2/12
 * @description
 */
public class JsoupHandler {
//    public static void main(String[] args) {
//        String htmlStr = "<html><head><title>Hello Jsoup</title></head><body></body></html>";
//        Document doc = Jsoup.parse(htmlStr);
        //选择器
        //select
        //        Elements elements =  doc.select("body");
//        Elements elements = doc.select("body");
//        Element element = doc.getElementById("");
//public static void main(String[] args) {
//    getData();
//}
    public static ArrayList<DataBean> getData(){
        ArrayList<DataBean> result = new ArrayList<>();
        try {
            //直接传递url获取返回结果
            Document document = Jsoup.connect(" https://ncov.dxy.cn/ncovh5/view/pneumonia?scene=2&from=singlemessage&isappinstalled=0 ").get();
            Element element = document.getElementById("getAreaStat");
            String data = element.data();
            //通过截取中括号的方式拿到json数据
            String subData = data.substring(data.indexOf("["),data.lastIndexOf("]")+1);

            Gson gson = new Gson();
            ArrayList list = gson.fromJson(subData,ArrayList.class);

            for (int i = 0; i < list.size(); i++) {
                Map map = (Map)list.get(i);
                String name = (String)map.get("provinceName");
                double confirm = (Double)map.get("confirmedCount");
                double heal = (Double)map.get("curedCount");
                double dead = (Double)map.get("deadCount");
                DataBean dataBean = new DataBean(null,name,0,(int)confirm,(int)heal,(int)dead);
                result.add(dataBean);

            }
            System.out.println();

        }catch (Exception e) {

        }
        return result;

    }
}
